﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace CAPlugin.Interface
{
    public interface IClient
    {
        /// <summary>
        /// 获取SDK版本
        /// </summary>
        /// <returns></returns>
        string GetVersion();

        /// <summary>
        /// 设置证书过滤策略
        /// 该接口应该在 GetUserList之前调用
        /// </summary>
        /// <param name="appPolicy">证书过滤策略 "SIGN"签名证书 ， "ENCRYPT" 加密证书 </param>
        void SetCertAppPolicy(string appPolicy);

        /// <summary>
        /// 设置生成摘要hash值的算法
        /// "MD5"、"SHA1"、"SM3"、"SHA256"
        /// </summary>
        int SetHashMethod(string hashMenthod);

        /// <summary>
        /// 获取摘要hash算法
        /// </summary>
        /// <returns></returns>
        string GetHashMentod();

        /// <summary>
        /// 设置加密算法
        /// </summary>
        /// <param name="encryptMethod">“DES_CBC”，“DES_ECB”，“3DES_CBC”，“3DES_ECB”，“SM1_CBC”，“SM1_ECB”，“AES128_CBC”“AES256_CBC”</param>
        /// <returns>0:成功 其他：失败</returns>
        int SetEncryptMethod(string encryptMethod);

        /// <summary>
        /// 获取加密算法
        /// </summary>
        /// <returns></returns>
        string GetEncryptMethod();

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns>
        /// 用户之间 &&& 分割 eg: 用户&&&用户&&& 用户&&& 用户
        /// 用户的属性 || 分割  eg: CertID || Subject || IssuerSubject || CertBase64
        /// </returns>
        string GetUserList();

        /// <summary>
        /// 导出用户证书的公钥(Base64字符串)
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <returns></returns>
        string ExportUserCert(string certId);

        /// <summary>
        /// 获取证书信息
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="sType">信息编号,固定格式 eg:0x00000001(证书版本)</param>
        /// <returns></returns>
        string GetCertInfo(string certId, short sType);

        /// <summary>
        /// 获得证书扩展信息
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="oid">证书oid</param>
        /// <returns></returns>
        string GetCertInfoByOid(string certId, string oid);

        /// <summary>
        /// 验证证书有效性
        /// </summary>
        /// <param name="certId"></param>
        /// <returns></returns>
        int ValidateCert(string certId);

        /// <summary>
        /// 数据签名
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inData">需要签名的数据</param>
        /// <returns></returns>
        string SignData(string certId, string inData);

        /// <summary>
        /// 数据验签
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inData">待验证数据</param>
        /// <param name="inDataLength">待验证数据长度</param>
        /// <param name="signValue">签名值</param>
        /// <returns></returns>
        int VerifySignedData(string certId, string inData, int inDataLength, string signValue);

        /// <summary>
        /// 文件签名
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inFile">待签名文件</param>
        /// <returns></returns>
        string SignFile(string certId, string inFile);


        /// <summary>
        /// 文件验签
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inFile">待验证文件</param>
        /// <param name="signValue">签名值</param>
        /// <returns></returns>
        int VerifySignedFile(string certId, string inFile, string signValue);

        /// <summary>
        /// 对称加密
        /// </summary>
        /// <param name="symmkey">加密密钥</param>
        /// <param name="inData">待加密数据</param>
        /// <returns></returns>
        string EncryptData(string symmKey, string inData);

        /// <summary>
        /// 对称解密
        /// </summary>
        /// <param name="symmKey">密钥</param>
        /// <param name="inData">待解密数据</param>
        /// <returns></returns>
        string DecryptData(string symmKey, string inData);

        /// <summary>
        /// 文件对称加密
        /// </summary>
        /// <param name="symmKey">密钥</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        int EncryptFile(string symmKey, string inFile, string outFile);

        /// <summary>
        /// 文件对称解密
        /// </summary>
        /// <param name="symmKey">密钥</param>
        /// <param name="inFile">待解密文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns>0：成功  其他：失败</returns>
        int DecryptFile(string symmKey, string inFile, string outFile);

        /// <summary>
        /// 使用公钥加密数据
        /// </summary>
        /// <returns></returns>
        string PubKeyEncrypt(string certId, string inData);

        /// <summary>
        /// 使用私钥解密数据
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inData">待解密数据</param>
        /// <returns></returns>
        string PriKeyDecrypt(string certId, string inData);

        /// <summary>
        /// PKCS#7 标准对数据签名
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inData">待签名数据</param>
        /// <returns></returns>
        string SignDataByP7(string certId, string inData);

        /// <summary>
        /// PKCS#7 验签
        /// </summary>
        /// <param name="P7Data">P7签名值</param>
        /// <param name="srcData">待验证数据</param>
        /// <returns></returns>
        int VerifySignedDataByP7(string P7Data, string srcData);

        /// <summary>
        /// 获取P7签名信息
        /// </summary>
        /// <param name="P7Data">签名值</param>
        /// <param name="sType">信息类型：  0：原文信息、1：颁发者信息、2：序列号、3：P1签名数据、4：签名证书</param>
        /// <returns></returns>
        string GetP7SignDataInfo(string P7Data, short sType);

        /// <summary>
        /// XML签名
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inData">代签名xml字符串</param>
        /// <returns></returns>
        string SignDataXML(string certId, string inData);

        /// <summary>
        /// XML验签
        /// </summary>
        /// <param name="inData"></param>
        /// <returns></returns>
        int VerifySignedDataXML(string inData);

        /// <summary>
        /// 获取XML签名信息
        /// </summary>
        /// <param name="xmlSignedData">xml签名值</param>
        /// <param name="sType">信息类型：1：原文、2：摘要数据、3：P1签名结果、4：签名证书、5：摘要算法、6：签名算法</param>
        /// <returns></returns>
        string GetXMLSignatureInfo(string xmlSignedData, short sType);

        /// <summary>
        /// 设置PKCS#7 模式
        /// </summary>
        /// <param name="sMode">0：attach模式；1：detach模式</param>
        /// <returns></returns>
        int SetP7SignMode(short sMode);

        /// <summary>
        /// 加密为数字信封
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inData">待加密数据</param>
        /// <returns></returns>
        string EnvelopeEncrypt(string certId, string inData);

        /// <summary>
        /// 数字信封解密
        /// </summary>
        /// <param name="inData">待解密数据</param>
        /// <returns></returns>
        string EnvelopeDecrypt(string inData);

        /// <summary>
        /// 加密签名数字信封
        /// </summary>
        /// <param name="signedCertID">签名过的certID</param>
        /// <param name="encryptedCertId">对称加密过的certID</param>
        /// <param name="inData">输入数据</param>
        /// <returns></returns>
        string SignEnvelopeEncrypt(string signedCertID,string encryptedCertId,string inData);

        /// <summary>
        /// 解密待签名的数字信封
        /// </summary>
        /// <param name="inData">待解密数据</param>
        /// <returns></returns>
        string SignEnvelopeDecrypt(string inData);

        /// <summary>
        /// 将文件内容加密为数字信封，并输出到指定文件   
        /// </summary>
        /// <param name="signedCertId">签名过的certID</param>
        /// <param name="encryedCertId">对称加密的CertID</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        int EnvelopeEncryptFromFile(string certId, string inFile, string outFile);

        /// <summary>
        /// 从文件中解密数字信封，并输出到指定文件
        /// </summary>
        /// <param name="inFile">待解密信封文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        int EnvelopeDecryptFromFile(string inFile, string outFile);

        /// <summary>
        /// 将文件内容加密成带有签名的信封
        /// </summary>
        /// <param name="signedCertId">签名后的certID</param>
        /// <param name="encryedCertId">对称加密后的CertId</param>
        /// <param name="inFile">输入文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns></returns>
        int SignEnvelopeEncryptFromFile(string signedCertId, string encryedCertId, string inFile, string outFile);

        /// <summary>
        /// 解密文件内带有签名的数字信封
        /// </summary>
        /// <param name="inFile"></param>
        /// <param name="outFile"></param>
        /// <returns></returns>
        int SignEnvelopeDecryptFromFile(string inFile, string outFile);

        /// <summary>
        /// 进行Base64编码
        /// </summary>
        /// <param name="inData">待编码数据</param>
        /// <returns></returns>
        string Base64Encode(string inData);

        /// <summary>
        /// Base64解码
        /// </summary>
        /// <param name="inData">待解码数据</param>
        /// <returns></returns>
        string Base64Decode(string inData);

        /// <summary>
        /// P7 attach模式下 验签
        /// </summary>
        /// <param name="inData">待验签数据</param>
        /// <returns></returns>
        string VerifyAttachSigned(string inData);

        /// <summary>
        /// P7文件签名
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inFile">待签名文件</param>
        /// <param name="outFile">签名值输出文件</param>
        /// <returns>0 成功 其他失败</returns>
        int P7SignFromFile(string certId, string inFile, string outFile);

        /// <summary>
        ///  P7文件验签
        /// </summary>
        /// <param name="inFile">待验签文件</param>
        /// <param name="outFile">签名后文件</param>
        /// <returns>0成功，其他失败</returns>
        int P7VerifyFromFile(string inFile, string outFile);

        /// <summary>
        /// 使用公钥加密文件
        /// </summary>
        /// <param name="cetrId">证书序列号</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns>0成功，其他失败</returns>
        int PubKeyEncryptFile(string cetrId, string inFile, string outFile);

        /// <summary>
        /// 使用私钥解密文件
        /// </summary>
        /// <param name="certId">证书ID</param>
        /// <param name="inFile">待解密文件</param>
        /// <param name="outFile">解密后的文件</param>
        /// <returns>0成功，其他失败</returns>
        int PriKeyDecryptFile(string certId, string inFile, string outFile);

        /// <summary>
        /// 获取错误信息
        /// </summary>
        /// <returns></returns>
        string GetErrorMsg();

        /// <summary>
        /// 修改UsbKey Pin码
        /// </summary>
        /// <param name="oldPin"></param>
        /// <param name="newPin"></param>
        /// <returns></returns>
        int ModifyUsbKeyPIN(string oldPin, string newPin);

        /// <summary>
        /// 设置证书过滤条件
        /// </summary>
        /// <param name="certFilterFlag"></param>
        /// <param name="certFilter"></param>
        /// <returns>0成功，其他失败</returns>
        int SetCertFilter(int certFilterFlag, string certFilter);

        /// <summary>
        /// ca证书登录
        /// </summary>
        /// <param name="certId"></param>
        /// <param name="userPin"></param>
        /// <returns></returns>
        int Login(string certId, string userPin);

        /// <summary>
        /// 多人会签
        /// 该接口可以反复调用，再次签名时，bstrInFile请传入上次签完输出的文件，bstrOutFile为标准pkcs7格式二进制文件。
        /// 注意：该接口无法使用Detach方式(不附加原文)
        /// </summary>
        /// <param name="certId">证书序列号</param>
        /// <param name="inFile">待签文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns>0成功，其他失败</returns>
        int P7SignFromFileEx(string certId, string inFile, string outFile);

        /// <summary>
        /// 多人会签眼前
        /// 注意：该接口无法使用Detach方式 ，多人会签接口，必须使用P7的Attach模式
        /// </summary>
        /// <param name="inFile">多人会签最后的签名值</param>
        /// <param name="outFile">所有签名都脱签后的原始文件</param>
        /// <returns>验签通过的所有证书的DN名（用“&&&”分隔的字符串）</returns>
        string P7VerifyFromFileEx(string inFile,string outFile);

        /// <summary>
        /// 获取证书信息 
        /// </summary>
        /// <param name="strBase64">base64编码的证书</param>
        /// <param name="sType">信息类型编码 eg：0x00000001 //证书版本</param>
        /// <returns></returns>
        string GetCertInfoEx(string strBase64, short sType);

        /// <summary>
        /// 通过证书的base64实体与OID参数获取OID属性的值
        /// </summary>
        /// <param name="strBase64">证书的base64</param>
        /// <param name="strOid">属性OID</param>
        /// <returns></returns>
        string GetCertInfoByOidEx(string strBase64, string strOid);

        /// <summary>
        /// 检测密码服务提供者
        /// </summary>
        /// <returns>0成功，其他失败</returns>
        int CheckHH();

        /// <summary>
        /// 生成hash摘要
        /// </summary>
        /// <param name="inData"></param>
        /// <returns></returns>
        string HashData(string inData);

        /// <summary>
        /// 获取设备序列号
        /// </summary>
        /// <param name="certID">证书序列号</param>
        /// <returns>设备序列号（16位固串）</returns>
        string GetKeySN(string certID);

        /// <summary>
        /// 获取系统证书库中的证书对的个数
        /// 过滤掉无法正常注册到系统证书库的证书
        /// 过滤掉发行者CN不为HBCA的证书
        /// 过滤掉没有对应私钥的证书
        /// 过滤掉证书用法（签名和加密）不成对的证书
        /// 保留了保留p12软证书
        /// </summary>
        /// <returns>系统证书库中的证书对的个数</returns>
        int GetCertCount();

        /// <summary>
        /// 使用公钥对明文加密
        /// </summary>
        /// <param name="certB64">证书公钥的Base64</param>
        /// <param name="inData">明文字符串</param>
        /// <returns></returns>
        string PubKeyEncryptEx(string certB64, string inData);

        /// <summary>
        /// 数字信封加密
        /// </summary>
        /// <param name="certB64">证书序号的Base64</param>
        /// <param name="inData">待加密数据</param>
        /// <returns></returns>
        string EnvelopeEncryptEx(string certB64, string inData);

        /// <summary>
        /// 选择证书
        /// </summary>
        /// <returns></returns>
        string SelectCert();

        /// <summary>
        /// 清除针对SM2证书保存的PIN码
        /// 只对SM2证书有效
        /// 过滤掉发行者CN不是HBCA的
        /// 过滤掉没有对应私钥的证书
        /// 过滤掉证书用法(签名和加密)不成对的证书
        /// 保留p12软证书
        /// </summary>
        /// <returns></returns>
        int ClearPinCache();

        /// <summary>
        /// 产生给定长度随机整数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        string GenRandom(short length);
    }
}
